<?php
/**
 * Copyright 2011  SURFfoundation
 * 
 * This file is part of ESCAPE.
 * 
 * ESCAPE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * ESCAPE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with ESCAPE.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * For more information:
 * http://escapesurf.wordpress.com/
 * http://www.surffoundation.nl/
 * 
 * Contact: d.vierkant@utwente.nl
 * 
 * @license http://www.gnu.org/licenses/gpl.html GNU GPLv3
 */
?>
<?php
/**
 * Rem
 * 
 * Show a resource map.
 */
class escape_repositoryui_action_Rem extends escape_repositoryui_Action
{
	public function handleRequest()
	{
		/* @var $repository escape_repository_Repository */
		$repository =& $this->requestAttributes['repository'];
				
		// fetch the ID from the request
		$aggregationId = null;
		if(preg_match('/^.*:([^:]+)$/', $_REQUEST['actionValue'], $matches))
		{
			// the ID contains a ":", strip everything upto including the ":"
			// PIDs are used instead of IDs by the Fedora service calls, so we need to be able to accept these as well
			$aggregationId = $matches[1];
		}
		else
		{
			$aggregationId = $_REQUEST['actionValue'];
		}
		$aggregationUri = $repository->convertDataObjectIdToObjectUri($aggregationId);
		
		$format = $_REQUEST['format'];
		$titlePredicate = $_REQUEST['title_predicate'];
		
		// fetch the aggregation object
		try {
			$aggregationObject = $repository->getOreAggregationByUri($aggregationUri);
		}
		catch(Exception $exception)
		{
			// the specified aggregation does not exist
			header("HTTP/1.1 404 Not Found");
			header("Status: 404 Not Found");
			header('Content-Type: text/plain');
			print "Resource Map not found!\n";
			
			return null;
		}
		
		// fetch the REM document
		$remDocument = $aggregationObject->generateResourceMapDocument($titlePredicate);
		
		// check the format
		if($format === 'application/json')
		{
			// send the headers
			header('Content-Type: application/json; charset=UTF-8');
			header('Content-Disposition: inline; filename="' . $aggregationId . '.js"');
			
			// without these headers Internet Explorer will cache the JSON REM causing the visualizer to show old versions of the aggregation
			header('CacheControl: no-cache', true);
			header('Pragma: no-cache', true);
			header('Expires: -1', true);
			
			// send the REM document
			print $remDocument->getRdfJson();
		}	
		else
		{
			// send the headers
			header('Content-Type: application/rdf+xml; charset=UTF-8');
			header('Content-Disposition: inline; filename="' . $aggregationId . '.rdf"');
			
			// send the REM document
			print $remDocument->getRdfXml();
		}

		return null;
	}
}